﻿namespace Growatt.ShinebusServiceUploadPack.Service.OSS
{
    using Growatt.ShinebusServiceUploadPack.Controllers;
    using Growatt.ShinebusServiceUploadPack.Service.OSS.新文件夹;
    using Newtonsoft.Json;
    using System;
    using System.Net.Http;
    using System.Text;
    using System.Threading.Tasks;

    public class GetAllDeviceTypes
    {
        private readonly IHttpClientFactory _httpClientFactory;

        public GetAllDeviceTypes(IHttpClientFactory httpClientFactory)
        {
            _httpClientFactory = httpClientFactory;
        }
        public  async Task<GetAllDeviceTypesResponse> GetAllDeviceTypesAsync(ILogger<UpdatePackController> logger,  CancellationToken cancellationToken,string Server = "20.6.1.140:8081")
        {
            using (HttpClient client = _httpClientFactory.CreateClient())
            {
                var content = new FormUrlEncodedContent(new[]
                {
                new KeyValuePair<string, string>("op", "getDevieceTypeAndModel"),
                new KeyValuePair<string, string>("lang", "cn")
            });

                HttpResponseMessage response = await client.PostAsync($"http://{Server}/pubData.do", content, cancellationToken);

                if (response.IsSuccessStatusCode)
                {
                    string responseBody = await response.Content.ReadAsStringAsync();
                    GetAllDeviceTypesResponse result = JsonConvert.DeserializeObject<GetAllDeviceTypesResponse>(responseBody);
                    return result;
                }
                else
                {
                      logger.LogInformation("获取所有设备类型和设备机型失败: " + response.StatusCode);
                }
            }
            return null;

        }
    }

}
